import argparse

def read_bed(input_file):
    sites={}
    example=0
    with open(input_file, 'r') as rf:
        for line in rf:
            words = line.strip().split("\t")
            key=(words[0],words[1],words[5])
            test_value= 1 if int(words[10])>50 else 0
            sites[key]=test_value
            if example<5:
                print(f'example bed key: {key}')
                example+=1
    return sites

def parse_args():
    parser = argparse.ArgumentParser()
    parser.add_argument(
        "--singleton",
        type=str,
        default="/public/home/hpc234701005/data/R10.4/20221109_1654_5D_PAG68757_39c39833/train1/singleton.bisulfite.bed",
        help="singleton address.",
    )
    parser.add_argument(
        "--non-singleton",
        type=str,
        default="/public/home/hpc234701005/data/R10.4/20221109_1654_5D_PAG68757_39c39833/train1/non_singleton.bisulfite.bed",
        help="non-singleton address.",
    )
    parser.add_argument('--input_path', '-i',  type=str, default="/public/home/hpc234701005/data/R10.4/20221109_1654_5A_PAG65784_f306681d/test1/fast5s.CG.call_mods.frequency.0.0.bed",
                        help='a result file from call_modifications.py.')
    return parser.parse_args()

def pred_error(call_mods,singleton,non_singleton):
    all_error=0
    singleton_error=0
    singleton_error_t=0#The number of negative samples tends to be predicted as positive
    non_singleton_error_t=0
    singleton_error_f=0#The number of positive samples tends to be predicted as negative
    non_singleton_error_f=0
    singleton_num=0
    non_singleton_num=0
    error=0
    positive=0
    with open(call_mods, "r") as f:
        for line in f:
            words = line.strip().split()
            key=(words[0],words[1],words[5])
            #prob_0=float(words[6])
            #prob_1=float(words[7])
            pred=int(words[10])
            if key in singleton.keys():
                singleton_num+=1
                true_value=singleton[key]
                if pred>50 and true_value==0:
                    all_error+=1
                    singleton_error+=1
                    singleton_error_t+=1
                elif pred<=50 and true_value==1:
                    all_error+=1
                    singleton_error+=1
                    singleton_error_f+=1
                else:
                    positive+=1                  
            elif key in non_singleton.keys():
                non_singleton_num+=1
                true_value=non_singleton[key]
                if pred>50 and true_value==0:
                    all_error+=1
                    non_singleton_error_t+=1
                elif pred<=50 and true_value==1:
                    all_error+=1
                    non_singleton_error_f+=1
                else:
                    positive+=1
            #else:
            #    print(f'{key}')
            #    error+=1
            #    if error>=5:
            #        print(f'singleton total num : {len(singleton.keys())}')
            #        print(f'non-singleton total num : {len(non_singleton.keys())}')
            #        break
    print(f'pred true num: {positive}')
    print(f'singletons : non-singletons = {singleton_num} : {non_singleton_num}')
    print(f'singleton error account {singleton_error/all_error} of total errors')
    print(f'singleton FP account {singleton_error_f/singleton_error} of total singleton errors')
    print(f'non-singleton FP account {non_singleton_error_f/(all_error-singleton_error)} of total non-singleton errors')

if __name__ == '__main__':
    args=parse_args()
    singleton=read_bed(args.singleton)
    non_singleton=read_bed(args.non_singleton)
    pred_error(args.input_path,singleton,non_singleton)